package com.mqb.n8;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

import static com.mqb.n2.util.Sleeper.sleep;

/**
 * @author mqb
 * @date 2021/5/3 21:23
 */
@Slf4j(topic = "c.TestCyclicBarrier")
public class TestCyclicBarrier {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2);

        CyclicBarrier barrier = new CyclicBarrier(2);
//        CyclicBarrier barrier = new CyclicBarrier(2,()->{
//            log.debug("task1 task2 finished");
//        });

        pool.submit(()->{
            log.debug("task1 begin...");
            sleep(1);
            try {
                barrier.await();
                log.debug("task1 end...");
            } catch (InterruptedException|BrokenBarrierException e) {
                e.printStackTrace();
            }
        });

        pool.submit(()->{
            log.debug("task2 begin...");
            sleep(2);
            try {
                barrier.await();
                log.debug("task2 end...");
            } catch (InterruptedException|BrokenBarrierException e) {
                e.printStackTrace();
            }
        });
    }
}
